package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import xyz.hubery.domain.AdminBank;
import xyz.hubery.dto.AdminBankDTO;
import xyz.hubery.feign.AdminBankServiceFeign;
import xyz.hubery.model.R;
import xyz.hubery.service.AdminBankService;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/adminBanks")
@Api(tags = "银行卡管理")
public class AdminBankController implements AdminBankServiceFeign {

    @Autowired
    private AdminBankService bankService;


    @GetMapping
    @ApiOperation(value = "条件查询公司银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankCard" ,value = "公司的银行卡") ,
            @ApiImplicitParam(name = "current" ,value = "当前页") ,
            @ApiImplicitParam(name = "size" ,value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('admin_bank_query')")
    public R<Page<AdminBank>> findByPage(@ApiIgnore Page<AdminBank> page , String bankCard){
        Page<AdminBank> adminBankPage = bankService.page(page, new LambdaQueryWrapper<AdminBank>().like(!StringUtils.isEmpty(bankCard), AdminBank::getBankCard, bankCard));
        return R.ok(adminBankPage);
    }


    @PostMapping
    @ApiOperation(value = "新增一个银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank" ,value = "adminBank json 数据")
    })
    @PreAuthorize("hasAuthority('admin_bank_create')")
    public R add(@RequestBody @Validated AdminBank adminBank){
        boolean save = bankService.save(adminBank);
        if(save){
            return R.ok() ;
        }
        return R.fail("新增失败") ;
    }

    @PatchMapping
    @ApiOperation(value = "修改一个银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank" ,value = "adminBank json 数据")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R update(@RequestBody @Validated AdminBank adminBank){
        boolean updateById = bankService.updateById(adminBank);
        if(updateById){
            return R.ok() ;
        }
        return R.fail("修改失败") ;
    }

    @PostMapping("/adminUpdateBankStatus")
    @ApiOperation(value = "修改银行卡的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId" ,value = "要修改的银行卡的 ID"),
            @ApiImplicitParam(name = "status" ,value = "要修改为的状态")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R changeStatus(Long bankId ,Byte status){
        AdminBank adminBank = new AdminBank();
        adminBank.setId(bankId);
        adminBank.setStatus(status);
        boolean updateById = bankService.updateById(adminBank);
        if(updateById){
            return R.ok() ;
        }
        return R.fail("状态修改失败") ;
    }

    @GetMapping("/list")
    @Override
    public List<AdminBankDTO> listAdminBanks() {
        return bankService.listAdminBanks();
    }
}
